home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 1 of 3.iso
/
chapte25
/
ex18.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-10-28
|
4KB
|
98 lines
#include <genstub.c>
#define IDM_PULSE 500
DWORD WINAPI ChildThreadProc( HWND hWnd )
{
HWND hThreadWnd; // Handle of window for this thread.
HANDLE hEvent; // Handle to event.
TCHAR szBuffer[101]; // General work area for string formatting.
wsprintf( szBuffer, "Thread %lX", GetCurrentThreadId() );
hThreadWnd = CreateWindow( lpszAppName, szBuffer, WS_OVERLAPPEDWINDOW, 0, 0,
100, 100, (HWND)NULL, NULL, hInst, (LPTSTR)NULL );
if (!hThreadWnd)
return (FALSE);
// Display the window and open the "PULSE" event.
ShowWindow( hThreadWnd, SW_SHOWNORMAL );
UpdateWindow( hThreadWnd );
hEvent = OpenEvent( 0, FALSE, "PULSE" );
while (TRUE) // Loop forever going into and out of wait state.
{
MSG msg; // Used for cleaning off queue.
DWORD dwTest; // Used for testing the return value of wait function.
while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
wsprintf( szBuffer, "Thread %x waiting for Event %x or Mouse Button Down.",
GetCurrentThreadId(), hEvent );
SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
// Check for manual event or mouse button pressed.
dwTest = MsgWaitForMultipleObjects( 1, &hEvent, FALSE, 10000, QS_MOUSEBUTTON );
if (dwTest != WAIT_TIMEOUT)
wsprintf( szBuffer,"Thread %x satisfied condition.", GetCurrentThreadId() );
else
wsprintf( szBuffer,"Thread %x timed out.", GetCurrentThreadId() );
SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
}
CloseHandle( hEvent );
ExitThread( TRUE );
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HANDLE hEvent = 0;
switch ( uMsg )
{
case WM_CREATE:
hEvent = CreateEvent( NULL, TRUE, FALSE, "PULSE" );
return DefWindowProc( hWnd, uMsg, wParam, lParam );
case WM_COMMAND: // process menu items
switch ( LOWORD( wParam ) )
{
case IDM_TEST:
{
DWORD dwChildId;
CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &dwChildId );
}
break;
case IDM_PULSE:
hEvent = OpenEvent( SYNCHRONIZE, FALSE, "PULSE" );
SendMessage( hWnd, WM_USER, 0, (LPARAM)"Pulsing Event" );
PulseEvent( hEvent );
CloseHandle( hEvent );
break;
case IDM_EXIT:
DestroyWindow( hWnd );
break;
}
break;
case WM_USER:
{ // Show synchronization activity.
TCHAR szBuffer[101];
static int row = 0;
static int msg_num = 1;
HDC hDC = GetDC( hWnd );
FillMemory( szBuffer, 100, 32 );
szBuffer[100] = 0;
TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
row = ( row > 220 ) ? 0 : row + 20;
ReleaseDC( hWnd, hDC );
}
break;
case WM_DESTROY:
if ( hEvent )
CloseHandle( hEvent );
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
return NULL;
}